/*
 *  Copyright 2012 Dirk Vranckaert
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package eu.vranckaert.vvl.activities.trafficinfo;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import eu.vranckaert.vvl.R;
import eu.vranckaert.vvl.activities.DashboardActivity;
import eu.vranckaert.vvl.activities.test.MyActivityTestCase;
import eu.vranckaert.vvl.testutil.TestConstants;
import eu.vranckaert.vvl.utils.context.Preference;
import eu.vranckaert.vvl.utils.view.MultiSelectSpinner;

/**
 * User: DIRK VRANCKAERT
 * Date: 23/02/12
 * Time: 19:20
 */
public class TrafficInfoTextActivityTest extends MyActivityTestCase<TrafficInfoTextActivity> {
    private final String LOG_TAG = TrafficInfoTextActivityTest.class.getSimpleName();

    public TrafficInfoTextActivityTest() {
        super(TrafficInfoTextActivity.class);
    }

    /**
     * Tests the home-button in the top-bar of the activity.
     */
    public void testGoHome() {
        goHome();
    }

    public void testSelectionSpinners() {
        int selectedIndexInformation = 1;
        int selectedIndexRegion = 2;
        int selectedIndexHighroads = 0;
        int selectedIndexSecondaryRoads = 1;
        
        String allSelected = getActivity().getString(R.string.traffic_info_text_criteria_spinner_all_selected);
        String empty = "";
        String selectedSingleValueInformation = getActivity().getResources()
                .getTextArray(R.array.traffic_info_text_criteria_spinner_values_information)[selectedIndexInformation].toString();
        String selectedSingleValueRegion = getActivity().getResources()
                .getTextArray(R.array.traffic_info_text_criteria_spinner_values_region)[selectedIndexRegion].toString();
        String selectedSingleValueHighroads = getActivity().getResources()
                .getTextArray(R.array.traffic_info_text_criteria_spinner_values_highroad)[selectedIndexHighroads].toString();
        String selectedSingleValueSecondaryRoads = getActivity().getResources()
                .getTextArray(R.array.traffic_info_text_criteria_spinner_values_secondary_road)[selectedIndexSecondaryRoads].toString();
        
        solo.assertCurrentActivity("The traffic information TEXT activity is expected", TrafficInfoTextActivity.class);

        MultiSelectSpinner informationSpinner = (MultiSelectSpinner) getActivity().findViewById(R.id.traffic_info_text_spinner_information);
        MultiSelectSpinner regionSpinner = (MultiSelectSpinner) getActivity().findViewById(R.id.traffic_info_text_spinner_region);
        MultiSelectSpinner highroadsSpinner = (MultiSelectSpinner) getActivity().findViewById(R.id.traffic_info_text_spinner_highroads);
        MultiSelectSpinner secondaryRoadsSpinner = (MultiSelectSpinner) getActivity().findViewById(R.id.traffic_info_text_spinner_secondary_roads);

        assertEquals("At first all items should be selected", allSelected, (String) informationSpinner.getSelectedItem());
        assertEquals("At first all items should be selected", allSelected, (String) regionSpinner.getSelectedItem());
        assertEquals("At first no items should be selected", empty, (String) highroadsSpinner.getSelectedItem());
        assertEquals("At first no items should be selected", empty, (String) secondaryRoadsSpinner.getSelectedItem());

        Log.i(LOG_TAG, "Setting the selection for the information dialog and submit the selection");
        solo.clickOnView(informationSpinner);
        uncheckAllCheckboxesInInformationDialog();
        solo.clickOnMenuItem(selectedSingleValueInformation);
        solo.clickOnButton(getActivity().getString(R.string.ok));

        Log.i(LOG_TAG, "Setting the selection for the region dialog and submit the selection");
        solo.clickOnView(regionSpinner);
        uncheckAllCheckboxesInRegionDialog();
        solo.clickOnMenuItem(selectedSingleValueRegion);
        solo.clickOnButton(getActivity().getString(R.string.ok));

        Log.i(LOG_TAG, "Setting the selection for the highroad dialog and submit the selection");
        solo.clickOnView(highroadsSpinner);
        solo.clickOnMenuItem(selectedSingleValueHighroads);
        solo.clickOnButton(getActivity().getString(R.string.ok));

        Log.i(LOG_TAG, "Setting the selection for the secondary roads dialog and submit the selection");
        solo.clickOnView(secondaryRoadsSpinner);
        solo.clickOnMenuItem(selectedSingleValueSecondaryRoads);
        solo.clickOnButton(getActivity().getString(R.string.ok));
        Log.i(LOG_TAG, "Wanting to select item '" + selectedSingleValueSecondaryRoads + "'");

        Log.i(LOG_TAG, "Waiting 10 seconds for UI to be updated!");
        solo.waitForDialogToClose(TestConstants.Time.TEN_SECONDS);
        assertEquals(selectedSingleValueInformation, (String) informationSpinner.getSelectedItem());
        assertEquals(selectedSingleValueRegion, (String) regionSpinner.getSelectedItem());
        assertEquals(selectedSingleValueHighroads, (String) highroadsSpinner.getSelectedItem());
        assertEquals(selectedSingleValueSecondaryRoads, (String) secondaryRoadsSpinner.getSelectedItem());

        assertEquals("Only one text info type expected in the preferences", 1, Preference.getSelectedTextInformationTypes(getActivity()).size());
        assertEquals("Only one text region expected in the preferences", 1, Preference.getSelectedTextRegions(getActivity()).size());
    }

    public void testValidation() {
        String empty = "";
        int selectedIndexInformation = 3;
        int selectedIndexRegion = 2;

        String selectedSingleValueInformation = getActivity().getResources()
                .getTextArray(R.array.traffic_info_text_criteria_spinner_values_information)[selectedIndexInformation].toString();
        String selectedSingleValueRegion = getActivity().getResources()
                .getTextArray(R.array.traffic_info_text_criteria_spinner_values_region)[selectedIndexRegion].toString();
        
        MultiSelectSpinner informationSpinner = (MultiSelectSpinner) getActivity().findViewById(R.id.traffic_info_text_spinner_information);
        MultiSelectSpinner regionSpinner = (MultiSelectSpinner) getActivity().findViewById(R.id.traffic_info_text_spinner_region);

        solo.clickOnView(informationSpinner);
        uncheckAllCheckboxesInInformationDialog();
        solo.clickOnButton(getActivity().getString(R.string.ok));
        solo.clickOnView(regionSpinner);
        uncheckAllCheckboxesInRegionDialog();
        solo.clickOnButton(getActivity().getString(R.string.ok));

        Log.i(LOG_TAG, "Waiting 10 seconds for UI to be updated!");
        solo.waitForDialogToClose(TestConstants.Time.TEN_SECONDS);
        assertEquals("No items should be selected", empty, (String) informationSpinner.getSelectedItem());
        assertEquals("No items should be selected", empty, (String) regionSpinner.getSelectedItem());

        TextView validationTextView = (TextView) getActivity().findViewById(R.id.validation);

        assertTrue("The validation text view should be gone", validationTextView.getVisibility() == View.GONE);
        solo.clickOnButton(getActivity().getString(R.string.load_info));
        Log.i(LOG_TAG, "Waiting 10 seconds for UI to be updated!");
        solo.waitForDialogToClose(TestConstants.Time.TEN_SECONDS);
        assertTrue("The validation text view should be visible", validationTextView.getVisibility() == View.VISIBLE);

        solo.clickOnView(informationSpinner);
        solo.clickOnMenuItem(selectedSingleValueInformation);
        solo.clickOnButton(getActivity().getString(R.string.ok));

        solo.clickOnButton(getActivity().getString(R.string.load_info));
        Log.i(LOG_TAG, "Waiting 5 seconds for UI to be updated!");
        solo.waitForDialogToClose(TestConstants.Time.FIVE_SECONDS);
        assertTrue("The validation text view should still be visible", validationTextView.getVisibility() == View.VISIBLE);

        solo.clickOnView(regionSpinner);
        solo.clickOnMenuItem(selectedSingleValueRegion);
        solo.clickOnButton(getActivity().getString(R.string.ok));

        solo.clickOnButton(getActivity().getString(R.string.load_info));
        Log.i(LOG_TAG, "Waiting 5 seconds for UI to be updated!");
        solo.waitForDialogToClose(TestConstants.Time.FIVE_SECONDS);
        assertTrue("The validation text view should not be visible anymore", validationTextView.getVisibility() == View.GONE);
    }

    private void uncheckAllCheckboxesInInformationDialog() {
        Log.d(LOG_TAG, "Disabling all options for the information spinner");

        CharSequence[] options = getActivity().getResources().getTextArray(R.array.traffic_info_text_criteria_spinner_values_information);

        for (int i=0; i<options.length; i++) {
            solo.clickOnMenuItem(options[i].toString());
            solo.sendKey(KeyEvent.KEYCODE_DPAD_DOWN);
        }
    }

    private void uncheckAllCheckboxesInRegionDialog() {
        Log.d(LOG_TAG, "Disabling all options for the region spinner");

        CharSequence[] options = getActivity().getResources().getTextArray(R.array.traffic_info_text_criteria_spinner_values_region);

        for (int i=0; i<options.length; i++) {
            solo.clickOnMenuItem(options[i].toString());
            solo.sendKey(KeyEvent.KEYCODE_DPAD_DOWN);
        }
    }

    private void goHome() {
        solo.assertCurrentActivity("The traffic information TEXT activity is expected", TrafficInfoTextActivity.class);
        solo.clickOnImageButton(0);
        solo.assertCurrentActivity("The dashboard activity is expected", DashboardActivity.class);
        assertEquals(DashboardActivity.class, solo.getActivityMonitor().getLastActivity().getClass());
    }
}
